package tgl_services

import (
	"errors"
	"log"
	tgl_models "xiehui-service/app/models/tgl"
	"xiehui-service/global"
)

type tjlbService struct {
}

var TjlbService = new(tjlbService)

// 新增
func (tjlbService *tjlbService) Create(jlb tgl_models.Tjlb) (int64, error) {
	result := global.App.TglDB.Create(&jlb)
	if result.Error != nil {
		return 0, result.Error
	}
	return jlb.FNo, nil
}

func (tjlbService *tjlbService) GetOneInfo(FNo int64) (tjlbInfo *tgl_models.Tjlb, err error) {
	log.Println("err==>>>", global.App.TglDB)
	err = global.App.TglDB.First(tjlbInfo, FNo).Error
	log.Println("err==>>>", 222)
	log.Println("err==>>>", tjlbInfo, err)
	if err != nil {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
		return nil, err
	}
	return tjlbInfo, nil
}

func (tjlbService *tjlbService) Update(tjlb tgl_models.Tjlb) (err error) {
	if err := global.App.TglDB.Model(&tgl_models.Tjlb{}).Where("FNo = ?", tjlb.FNo).Omit("FNo", "Fguid").Updates(&tjlb).Error; err != nil {
		return err
	}
	return nil
}
